﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Agility.Extensions.DatabaseAccessor.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using OpenAdmin.Application.Services;
using OpenAdmin.Application.Services.Dtos;

namespace OpenAdmin.HttpApi.Controllers;

/// <summary>
/// 接口相关Api
/// </summary>
[ApiExplorerSettings(GroupName = "Application")]
public class ApiResourceController : ApiControllerBase
{
    private readonly IApiResourceService _apiResourceServices;
    /// <summary>
    /// 构造函数
    /// </summary>
    public ApiResourceController(IApiResourceService apiResourceServices)
    {
        _apiResourceServices = apiResourceServices;
    }

    /// <summary>
    /// 分页获取接口列表
    /// </summary>
    /// <param name="requestDto"></param>
    /// <returns></returns>
    [HttpGet, ProducesResponseType(typeof(PagedList<ApiResourcesByPageResponseDto>), 200)]
    public async Task<IActionResult> GetApiResourcesByPage([FromQuery] ApiResourcesByPageRequestDto requestDto)
    {
        return Ok(await _apiResourceServices.GetApiResourcesByPage(requestDto));
    }

    /// <summary>
    /// 获取全部api接口列表
    /// </summary>
    /// <returns></returns>
    [HttpGet, ProducesResponseType(typeof(List<ApiResourcesResponseDto>), 200)]
    public async Task<IActionResult> GetApiResources()
    {
        return Ok(await _apiResourceServices.GetApiResources());
    }

    /// <summary>
    /// 添加接口
    /// </summary>
    /// <param name="requestDto"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<IActionResult> AddApiResource(AddApiResourceRequestDto requestDto)
    {
        await _apiResourceServices.AddApiResource(requestDto);

        return Ok();
    }

    /// <summary>
    /// 更新接口信息
    /// </summary>
    /// <param name="requestDto"></param>
    [HttpPost]
    public async Task<IActionResult> EditApiResource(EditApiResourceRequestDto requestDto)
    {
        await _apiResourceServices.EditApiResource(requestDto);

        return Ok();
    }

    /// <summary>
    /// 删除接口
    /// </summary>
    /// <param name="id"></param>
    [HttpPost]
    public async Task<IActionResult> DeleteApiResource(int id)
    {
        await _apiResourceServices.DeleteApiResource(id);

        return Ok();
    }
}
